import 'package:flutter/material.dart';
import 'package:english_words/english_words.dart';
import 'package:basic_widgets/textEg.dart';
import 'package:basic_widgets/argsPage.dart';

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final wordPair = WordPair.random();
    return new MaterialApp(
        title: 'Startup Name Generator',
        initialRoute:"/",
        //注册路由表
        routes:{
          "new_page":(context) => MyDetail(text: '路由表',),
          "/":(context) => MainPage(title: wordPair.asPascalCase), //注册首页路由
          EchoRoute.routeName : (context) => EchoRoute(),
        } ,
        //home: RandomWords(),
        //指定了命名路由后，主页面需要注释掉
        //If the home property is specified, the routes table cannot include an entry for "/", since it would be redundant.
        //home: MainPage(title: wordPair.asPascalCase)
    );
  }
}

class MainPage extends StatefulWidget {
  MainPage({Key? key, required this.title}) : super(key: key);
  final String title;

  @override
  _MainPageState createState() => _MainPageState();
}

class _MainPageState extends State<MainPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Container(
            width: 300.0,
            height: 600.0,
            decoration: BoxDecoration(
              color: Colors.white,
              border: Border.all(
                color: Colors.grey,
                width: 9.0,
              ),
              borderRadius: const BorderRadius.all(const Radius.circular(8.0)),
            ),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Text(
                  WordPair.random().asCamelCase,
                  textAlign: TextAlign.center,
                  style: TextStyle(
                      fontWeight: FontWeight.bold,
                      fontSize: 20,
                      color: Colors.red),
                ),
                //传值和返回值
                TextButton(
                    onPressed: () async {
                      var result = await Navigator.push(
                        context,
                        MaterialPageRoute(
                          builder: (context) {
                            return MyDetail(
                              text: '我是提示xxxx',
                            );
                          },
                        ),
                      );
                      print("路由返回值: $result");
                    },
                    child: const Text('Detail')),
                //命名的路由跳转无参数传递
                TextButton(
                    onPressed: () async {
                      var result = await Navigator.pushNamed(
                        context,
                          "new_page"
                      );
                    },
                    child: const Text('Detail_Named')),
                //命名的路由跳转有参数传递（参数封装）
                TextButton(
                    onPressed: () {
                      Navigator.pushNamed(
                        context,
                        EchoRoute.routeName,
                        arguments: ScreenArguments('Named_with_args', 'named传参'),
                      );
                    },
                    child: const Text('Named_with_args')),
              ],
            )),
        //child: RandomWords(),
      ),
    );
  }
}

// class RandomWords extends StatefulWidget {
//   @override
//   _RandomWordsState createState() => _RandomWordsState();
// }
//
// class _RandomWordsState extends State<RandomWords> {
//   @override
//   final _suggestions = <WordPair>[];
//   final _biggerFont = const TextStyle(fontSize: 18.0);
//   @override
//   Widget build(BuildContext context) {
//     return Scaffold(
//       appBar: AppBar(
//         title: const Text('Startup Name Generator'),
//       ),
//       body: _buildSuggestions(),
//     );
//   }
//   Widget _buildRow(WordPair pair) {
//     return ListTile(
//       title: Text(
//         pair.asPascalCase,
//         style: _biggerFont,
//       ),
//     );
//   }
//   Widget _buildSuggestions() {
//     return ListView.builder(
//         padding: const EdgeInsets.all(16.0),
//         itemBuilder: /*1*/ (context, i) {
//           if (i.isOdd) return const Divider(); /*2*/
//
//           final index = i ~/ 2; /*3*/
//           if (index >= _suggestions.length) {
//             _suggestions.addAll(generateWordPairs().take(10)); /*4*/
//           }
//           return _buildRow(_suggestions[index]);
//         });
//   }
// }
